﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
public partial class View_ManageBusStation : System.Web.UI.Page
{
    private ManageBusStaion mnBusStation;
    private string SESSION_SEARCH = "SEARCH";
    protected void Page_Load(object sender, EventArgs e)
    {
        mnBusStation = new ManageBusStaion();
        if (!IsPostBack)
        {
            Session.Remove(SESSION_SEARCH);
        }
        ScriptManager.GetCurrent(Page).RegisterPostBackControl(tbnExport);
    }

    /// <summary>
    /// khuetd do du lieu vao bang
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void rdgBusStation_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
    {
        if ((Session[SESSION_SEARCH] != null) && (Session[SESSION_SEARCH].ToString() != string.Empty))
        {
            rdgBusStation.DataSource = mnBusStation.searchBusStationByKeyword(Session[SESSION_SEARCH].ToString());
        }
        else
        {
            rdgBusStation.DataSource = mnBusStation.getAllBusStation();
        }
    }

    /// <summary>
    /// khuetd : Tim kiem ben do
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        string stringSearch = txtKeyWord.Text.Trim();
        if (txtKeyWord.Text.Trim() == String.Empty)
        {
            stringSearch = hdfSearchString.Value.Trim();
        }

        if (Session[SESSION_SEARCH] != null)
        {
            if (hdfSearchString.Value.Trim() != Session[SESSION_SEARCH].ToString())
            {
                stringSearch = hdfSearchString.Value.Trim();
            }
            if (Session[SESSION_SEARCH].ToString() != txtKeyWord.Text.Trim())
            {
                stringSearch = txtKeyWord.Text.Trim();
            }
        }

        hdfSearchString.Value = stringSearch;
        rdgBusStation.DataSource = mnBusStation.searchBusStationByKeyword(stringSearch);
        rdgBusStation.Rebind();
        Session[SESSION_SEARCH] = stringSearch;
        txtKeyWord.Text = stringSearch;
    }

    /// <summary>
    /// ham do du lieu vao tung hang cua table
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void rdgBusStation_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
    {
        if (e.Item.IsInEditMode)
        {
            GridEditFormItem item = e.Item as GridEditFormItem;
            if (e.Item is GridEditFormInsertItem || e.Item is GridDataInsertItem)
            {
                Button btnAdd = new Button();
                btnAdd = item.FindControl("UpdateButton") as Button;
                btnAdd.Text = "Thêm mới";
                btnAdd.CssClass = "myButton";
                btnAdd.Attributes.Add("style", "margin-left:0px !important;");
                TextBox txtStationName = item.FindControl("txtStationName") as TextBox;
                String clientId = txtStationName.ClientID.ToString();
                txtStationNameHDF.Value = clientId;
                TextBox txtLongitude = item.FindControl("txtLongitude") as TextBox;
                txtLongitudehdf.Value = txtLongitude.ClientID.ToString();
                TextBox txtLatitude = item.FindControl("txtLatitude") as TextBox;
                txtLatitudehdf.Value = txtLatitude.ClientID.ToString();
            }
            else
            {
                Button btnUpdate = new Button();
                btnUpdate = item.FindControl("UpdateButton") as Button;
                btnUpdate.Text = "Chỉnh sửa";
                btnUpdate.CssClass = "myButton";
                btnUpdate.Attributes.Add("style", "margin-left:0px !important;");
            }
            Button btnCancel = new Button();
            btnCancel = item.FindControl("CancelButton") as Button;
            btnCancel.Text = "Hủy bỏ";
            btnCancel.CssClass = "myButton";
            ImageButton hypelink = item.FindControl("HyperLinkMap") as ImageButton;
            hypelink.CssClass = "myHyperLink";
        }
    }
    protected void rdgBusStation_InsertCommand(object sender, GridCommandEventArgs e)
    {
        GridEditableItem editedItem = e.Item as GridEditableItem;
        BusStation station = new BusStation();
        TextBox txtStationName = editedItem.FindControl("txtStationName") as TextBox;
        TextBox txtLongitude = editedItem.FindControl("txtLongitude") as TextBox;
        TextBox txtLatitude = editedItem.FindControl("txtLatitude") as TextBox;

        station.StaionName = txtStationName.Text;
        station.Longitude = txtLongitude.Text;
        station.Latitude = txtLatitude.Text;

        int _result = mnBusStation.insertNewBusStaion(station);
        if (_result >= 1)
            Message.Show(rapBusStation, "Thêm dữ liệu thành công!", Message.Type.Success);
        else if (_result == 0)
            Message.Show(rapBusStation, "Thêm dữ liệu không thành công", Message.Type.Error);
        else
            Message.Show(rapBusStation, "Thêm không thành công : Lỗi hệ thống", Message.Type.Error);
        rdgBusStation.CurrentPageIndex = 0;
    }
    protected void rdgBusStation_UpdateCommand(object sender, GridCommandEventArgs e)
    {
        int pageIndex = rdgBusStation.CurrentPageIndex;
        GridEditableItem editedItem = e.Item as GridEditableItem;
        BusStation station = new BusStation();
        int id;
        station.IDStation = int.TryParse(editedItem.OwnerTableView.DataKeyValues[e.Item.ItemIndex][BusStationColumn.IDStation].ToString(), out id) ? id : 0;

        TextBox txtStationName = editedItem.FindControl("txtStationName") as TextBox;
        TextBox txtLongitude = editedItem.FindControl("txtLongitude") as TextBox;
        TextBox txtLatitude = editedItem.FindControl("txtLatitude") as TextBox;

        station.StaionName = txtStationName.Text;
        station.Longitude = txtLongitude.Text;
        station.Latitude = txtLatitude.Text;

        int _result = mnBusStation.editBusStation(station);
        if (_result == 1)
            Message.Show(rapBusStation, "Chỉnh sửa dữ liệu thành công!", Message.Type.Success);
        else if (_result == 0)
            Message.Show(rapBusStation, "Chỉnh sửa dữ liệu không thành công", Message.Type.Error);
        else
            Message.Show(rapBusStation, "Chỉnh sửa dữ liệu không thành công : Lỗi hệ thống", Message.Type.Error);
        rdgBusStation.CurrentPageIndex = pageIndex;
    }
    protected void rdgBusStation_DeleteCommand(object sender, GridCommandEventArgs e)
    {
        int pageIndex = rdgBusStation.CurrentPageIndex;
        GridEditableItem editedItem = e.Item as GridEditableItem;
        BusStation station = new BusStation();
        int id;
        station.IDStation = int.TryParse(editedItem.OwnerTableView.DataKeyValues[e.Item.ItemIndex][BusStationColumn.IDStation].ToString(), out id) ? id : 0;

        int _result = mnBusStation.deleteBusStation(station);
        if (_result == 1)
            Message.Show(rapBusStation, "Xóa dữ liệu thành công!", Message.Type.Success);
        else if (_result == 0)
            Message.Show(rapBusStation, "Xóa dữ liệu không thành công", Message.Type.Error);
        else
            Message.Show(rapBusStation, "Xóa không thành công : Lỗi hệ thống", Message.Type.Error);
        rdgBusStation.CurrentPageIndex = pageIndex;
    }

    protected void tbnExport_Click(object sender, EventArgs e)
    {
        rdgBusStation.ExportSettings.Excel.Format = (GridExcelExportFormat)Enum.Parse(typeof(GridExcelExportFormat), "ExcelML");
        rdgBusStation.ExportSettings.IgnorePaging = true;
        rdgBusStation.ExportSettings.ExportOnlyData = true;
        rdgBusStation.ExportSettings.OpenInNewWindow = true;
        rdgBusStation.ExportSettings.FileName = "Danh-sach-diem-dung-" + DateTime.Now.ToString();
        rdgBusStation.MasterTableView.ExportToExcel();
    }
}